<?php
namespace Anls\Com\File;
use Anls\Common;

/**
 * 文件处理
 * @author w.
 *
 */
class FileHandle  {
	
    /**
     * 获取文件列表
     * @param string $symbolType
     * @param string $period
     */
    public static function getFilterFiles(){
        // 文件列表
        $fileList = array();
        $filePath = FileHandle::_getFilterFileCategoryPath();
        if (is_dir($filePath) && $dh = opendir($filePath)){
            $i = 0;
            while (($fileName = readdir($dh))!= false){
                if(strpos($fileName,".txt")){
                    $fileName = str_replace(".txt","",$fileName);
                    $fileList[$i] = $fileName;
                    $i++;
                }
            }
            closedir($dh);
        }
        if(count($fileList)==0){
            echo '获取文件失败';
        } else {
            echo '成功获取'. count($fileList) . '个文件</br>';
        }
        return $fileList;
    }
	
	/**
	 * 获取文件列表
	 * @param string $symbolType
	 * @param string $period
	 */
	public static function getFiles($symbolType,$period){
		// 文件列表
		$fileList = array();
		$filePath = FileHandle::_getFileCategoryPath($symbolType, $period);
		if (is_dir($filePath) && $dh = opendir($filePath)){
			$i = 0;
			while (($fileName = readdir($dh))!= false){
				if(strpos($fileName,".txt")){
					$fileName = str_replace(".txt","",$fileName);  
					$fileList[$i] = $fileName;
					$i++;
				}
			}
			closedir($dh);
		}
		if(count($fileList)==0){
			echo '获取文件失败';
		} else {
			echo '成功获取'. count($fileList) . '个文件</br>';
		}
		return $fileList;
	}
	
	/**
	 * 根据文件名，获取文件内容
	 * @param string $symbolType
	 * @param string $period
	 * @param string $fileName
	 */
	public static function getFileContentByFileName($symbolType,$period,$fileName){
		$rst = "";
		$filePath = FileHandle::_getFileCategoryPath($symbolType, $period). $fileName . ".txt";
		$fileSzie = filesize ($filePath);
		if($fileSzie>0){
			$handle = fopen($filePath, "r");
			$rst = fread($handle, filesize ($filePath));		
			fclose($handle);
		}
		return $rst;
	}
	
	/**
	 * 获取文件目录路径
	 * @param string $symbolType
	 * @param string $period
	 * @return string
	 */
	public static function _getFileCategoryPath($symbolType,$period){
		$dirFiles = '';
		// 原始数据路径
		if($symbolType == "stock"){
			$dirFiles = Common\Cfg::$STOCK_FILES_DIR;	
		}
		$filePath = $dirFiles. '//' . $period . '//';
		return $filePath;
	}
	
	/**
	 * 获取过滤代码文件目录路径
	 * @return string
	 */
	public static function _getFilterFileCategoryPath(){
	    
	    return Common\Cfg::$STOCK_FILTER_FILES_DIR . '//';
	}

}

?>
